package com.smartleanx.module.hrm.service.examine.examineDimensionIndex;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.smartleanx.module.hrm.controller.admin.examine.examineDimensionIndex.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineDimensionIndex.ExamineDimensionIndexDO;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.examine.examineDimensionIndex.ExamineDimensionIndexMapper;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 考核维度中间 Service 实现类
 *
 * @author 
 */
@Service
@Validated
public class ExamineDimensionIndexServiceImpl implements ExamineDimensionIndexService {

    @Resource
    private ExamineDimensionIndexMapper examineDimensionIndexMapper;

    @Override
    public Integer createExamineDimensionIndex(ExamineDimensionIndexSaveReqVO createReqVO) {
        // 插入
        ExamineDimensionIndexDO examineDimensionIndex = BeanUtils.toBean(createReqVO, ExamineDimensionIndexDO.class);
        examineDimensionIndexMapper.insert(examineDimensionIndex);
        // 返回
        return examineDimensionIndex.getId();
    }

    @Override
    public void updateExamineDimensionIndex(ExamineDimensionIndexSaveReqVO updateReqVO) {
        // 校验存在
        validateExamineDimensionIndexExists(updateReqVO.getId());
        // 更新
        ExamineDimensionIndexDO updateObj = BeanUtils.toBean(updateReqVO, ExamineDimensionIndexDO.class);
        examineDimensionIndexMapper.updateById(updateObj);
    }

    @Override
    public void deleteExamineDimensionIndex(Integer id) {
        // 校验存在
        validateExamineDimensionIndexExists(id);
        // 删除
        examineDimensionIndexMapper.deleteById(id);
    }

    private void validateExamineDimensionIndexExists(Integer id) {
        if (examineDimensionIndexMapper.selectById(id) == null) {
            throw exception(EXAMINE_DIMENSION_INDEX_NOT_EXISTS);
        }
    }

    @Override
    public ExamineDimensionIndexDO getExamineDimensionIndex(Integer id) {
        return examineDimensionIndexMapper.selectById(id);
    }

    @Override
    public PageResult<ExamineDimensionIndexDO> getExamineDimensionIndexPage(ExamineDimensionIndexPageReqVO pageReqVO) {
        return examineDimensionIndexMapper.selectPage(pageReqVO);
    }

}